package com.hp15c.calculator;

public class DisplayNumber {
	public static String format(String number) {
		String display = "";
		number = number.replace(".", ":").replace("-", "");
		String[] array = number.split(":");
		int size = 0;
		if (array.length == 2) {
			// DOT
			size = array[0].length();
			if (size > 3) {
				int next = 0;
				int loop = size / 3;
				int balance = array[0].length() % 3;
				if (balance > 0) {
					display = array[0].substring(0, balance);
					for (int i = 0; i < loop; i++) {
						next = balance + 3;
						display += "," + array[0].substring(balance, next);
						balance = next;
					}
				} else {
					for (int i = 0; i < loop; i++) {
						next = balance + 3;
						if (i == (loop - 1)) {
							display += array[0].substring(balance, next);
						} else {
							display += array[0].substring(balance, next) + ",";
						}
						balance = next;
					}
				}
				if (array.length > 1) {
					if (array[1] == null) {
						array[1] = "";
					}
					display = display + "." + array[1];
				} else {
					display = display + ".";
				}
				if (display.length() > 13) {
					display = display.substring(0, 13);
				}
			} else {
				if (array.length > 1) {
					if (array[1] == null) {
						array[1] = "";
					}
					display = array[0] + "." + array[1];
				} else {
					display = array[0] + ".";
				}
				if (display.length() > 12) {
					display = display.substring(0, 12);
				}
			}
		} else {
			// COMMA
			boolean replace = false;
			size = number.length();
			if (size > 0) {
				if (number.substring(number.length() - 1, number.length())
						.equals(":")) {
					number = number.replace(":", "");
					size = number.length();
					replace = true;
				}
				int next = 0;
				int loop = size / 3;
				int balance = number.length() % 3;
				if (balance > 0) {
					display = number.substring(0, balance);
					for (int i = 0; i < loop; i++) {
						next = balance + 3;
						display += "," + number.substring(balance, next);
						balance = next;
					}
				} else {
					for (int i = 0; i < loop; i++) {
						next = balance + 3;
						if (i == (loop - 1)) {
							display += number.substring(balance, next);
						} else {
							display += number.substring(balance, next) + ",";
						}
						balance = next;
					}
				}
			} else {
				display = number;
			}
			if (replace) {
				display += ".";
			}
			if (display.length() > 13) {
				display = display.substring(0, 13) + ",";
			}
		}
		return display;
	}

	public static String formatE(String number, boolean character) {
		String[] array = number.replace("-", "").split("E");
		int size = 0;
		if (array.length == 2) {
			if (character) {
				if (array[1].length() == 1) {
					size = Integer.valueOf(array[1]);
					array[1] = "WWWE0" + size;
				}
				if (array[1].length() == 2) {
					size = Integer.valueOf(array[1]);
					array[1] = "WWWE" + size;
				}
				if (array[1].length() == 3) {
					size = Integer.valueOf(array[1]);
					array[1] = "WWE" + size;
				}
				if (array[1].length() == 4) {
					size = Integer.valueOf(array[1]);
					array[1] = "WE" + size;
				}
			} else {
				if (array[1].length() == 2) {
					size = Integer.valueOf(array[1].substring(1, 2));
					array[1] = "WWW-0" + size;
				}
				if (array[1].length() == 3) {
					size = Integer.valueOf(array[1].substring(1, 3));
					array[1] = "WWW-" + size;
				}
				if (array[1].length() == 4) {
					size = Integer.valueOf(array[1].substring(1, 4));
					array[1] = "WW-" + size;
				}
			}
			if (size > 315) {
				return "Error";
			}
			if (array[0].length() > 6) {
				number = array[0].substring(0, 6) + array[1];
			} else {
				number = array[0].substring(0, array[0].length()) + array[1];
			}
			return number;
		}
		return null;
	}
}